package com.wn.sk.product.core.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 *  01 messageConverter() 这里是配置的是:
 *       发送哦消息的时候  会把 对象转成  json字符串
 *
 *  02  orderExchange() 订单交换机   交互机的名称：order-exchange
 *
 *  03  ttlOrderQueue() 订单队列   (进行 付款计时  未付款 到死信队列中)
 *
 *         params.put("x-message-ttl", 20000);           (计时)
 *         params.put("x-dead-letter-exchange", "order-dead-letter-exchange"); (信息到死信交互机中)
 *         params.put("x-dead-letter-routing-key", "dead.letter.order");       (信息到死信队列的路由)
 *
 *  04  Binding()  绑定 ( orderExchange 和 ttlOrderQueue 队列  )
 *
 *
 *  05  消息到死信队列中 (消息ttl到了 )
 *     创建死信交换机:  orderDeadLetterExchange()  交换机名称:order-dead-letter-exchange
 *     创建死信队列:   orderDeadLetterQueue()      队列名称:  order-dead-letter-queue
 *     绑定操作:      bindOrderDeadLetterQueue     把死信交换机和 死信队列绑定
 *
 */
@Configuration
public class RabbitConfig {

    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    // 交换机
    @Bean("orderExchange")
    public Exchange orderExchange() {
        return new TopicExchange("order-exchange", true, false);
    }
    // 队列
    @Bean("ttlOrderQueue")
    public Queue ttlOrderQueue() {
        Map params = new HashMap();
        params.put("x-message-ttl", 20000); // 20 秒 正常应该是 30min
        params.put("x-dead-letter-exchange", "order-dead-letter-exchange");
        params.put("x-dead-letter-routing-key", "dead.letter.order");
        return new Queue("ttl-order-queue", true, false, false, params);
    }
    // 绑定
    @Bean
    public Binding bindTTLOrderQueue(
            @Qualifier("ttlOrderQueue") Queue queue,
            @Qualifier("orderExchange") Exchange exchange) {
        return BindingBuilder.bind(queue).to(exchange)
                .with("order.#").noargs();
    }

    // 死信交换机
    @Bean("orderDeadLetterExchange")
    public Exchange orderDeadLetterExchange() {
        return new TopicExchange("order-dead-letter-exchange", true, false);
    }
    // 死信队列
    @Bean("orderDeadLetterQueue")
    public Queue orderDeadLetterQueue() {
        return new Queue("order-dead-letter-queue", true);
    }

    // 死信绑定
    @Bean
    public Binding bindOrderDeadLetterQueue(
            @Qualifier("orderDeadLetterQueue") Queue queue,
            @Qualifier("orderDeadLetterExchange") Exchange exchange) {
        return BindingBuilder.bind(queue)
                .to(exchange)
                .with("dead.letter.order").noargs();
    }


    /**
     *  需要 添加  RabbitAdmin (可以不用加 )
     * @param factory
     * @param orderExchange
     * @param orderDeadLetterExchange
     * @param ttlOrderQueue
     * @param orderDeadLetterQueue
     * @return
     */
    @Bean
    public RabbitAdmin rabbitAdmin(
            ConnectionFactory factory,
            Exchange orderExchange,
            Exchange orderDeadLetterExchange,
            Queue ttlOrderQueue,
            Queue orderDeadLetterQueue) {
        RabbitAdmin rabbitAdmin = new RabbitAdmin(factory);
        rabbitAdmin.setAutoStartup(true);
        rabbitAdmin.declareExchange(orderExchange);
        rabbitAdmin.declareExchange(orderDeadLetterExchange);
        rabbitAdmin.declareQueue(ttlOrderQueue);
        rabbitAdmin.declareQueue(orderDeadLetterQueue);
        return rabbitAdmin;
    }

}